<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22
 * Time: 11:26
 */
class Login extends CI_Controller{
    public $token_data;
    public function __construct(){
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('user_token_model');
        $this->token_data = check_token($this);
    }
    /**
     * 登录页面.
     */
    public function login(){ $this->load->view('user/login');}
    /**
     * 验证，验证成功进行信息更新
     */
    public function check(){
        //如果访问合法便进行数据操纵
        if($this->token_data['tag']){
            $data = $_POST['data'];
            $arr = json_decode($data,true);
            $arr['password'] = md5($arr['password']);
            $u =$this->user_model->findByName($arr['username']);
            if(isset($u)&&$u['status']==0){//如果没被停封且用户存在
                $user = $this->user_model->check($arr['username'], $arr['password']);
                if($user) {//账号密码相符
                    $this->updatetoken($user);//更新错误信息和token表数据
                    $id = $user['user_id'];
                    $user_token = $this->user_token_model->findById($id);
                    $this->token_data['user'] = $user_token['token'];//获取并保存token
                    $this->token_data['success'] = "登陆成功";
                    $this->token_data['login'] = "1";
                }else {//账号与密码不符
                    $user_id = $u['user_id'];
                    $user_wrong = $this->user_model->getUserById($user_id);//得到密码错误的用户
                    $this->wrong($user_wrong);//更新错误时间和次数
                }
            }else{
                $this->token_data['success'] = "该账号不存在或已被停封";
                $this->token_data['login'] = "0";
            }
        }
        $json = json_encode($this->token_data);
        echo $json;
    }

    /**
     * 退出
     */
    public function logout(){
        if($this->token_data['tag']){
            //只需在js里清空localStorage.user和localStorage.menu
        }
        $json = json_encode($this->token_data);
        echo $json;
    }

    /**
     * @param $user
     * 登录成功进行的信息更新
     */
    public function updatetoken($user){
        $data_wrong = array(
            'user_id' => $user['user_id'],
            'count' => 0,
            'time' => 0
        );
        $this->user_model->update_user($data_wrong);//清除错误信息
        //如果user_token表里没有这个用户,新建
        $have=$this->user_token_model->findById($user['user_id']);
        unset($user['password']);//不向token里存密码
        if(!$have){//如果token表没有该用户信息
            $new = array(
                'user_id'=>$user['user_id'],
                'token'=>json_encode($user)
            );
            $this->user_token_model->insert($new);
        }
        $data_token = array(//更新时间
            'user_id' => $user['user_id'],
            'token'=>json_encode($user),
            'update_time' => date("Y-m-d H:i:s"),
            'expire_time' => date("Y-m-d H:i:s", strtotime("+1 weeks")),
        );
        $this->user_token_model->update($data_token);
    }
    /**
     * @param $user_wrong
     * 更新错误时间和次数、进行错误的处理
     */
    public function wrong($user_wrong){
        if (strtotime(date("Y-m-d H:i:s")) - strtotime($user_wrong['time']) < 3600) {//如果距离上次错误间隔小于一小时
            if($user_wrong['count']>=4){
                $update_user = array(
                    'user_id' => $user_wrong['user_id'],
                    'status' => 1
                );
                $this->user_model->update_user($update_user);
                $this->token_data['success'] = "你的账号已被停封，如需找回请联系管理员";
                $this->token_data['login'] = "0";
            }else{
                $wrong = array(
                    'user_id' =>$user_wrong['user_id'],
                    'count' => $user_wrong['count'] + 1,
                    'time' => date("Y-m-d H:i:s")
                );
                $this->user_model->update_user($wrong);
                $www= $wrong['count'];
                $this->token_data['success'] = "密码连续错误".$www."次";
                $this->token_data['login'] = "0";
            }
        }
        else{
            $wrong = array(
                'user_id' => $user_wrong['user_id'],
                'count' => 1,
                'time' =>date("Y-m-d H:i:s"),
            );
            $this->user_model->update_user($wrong);
            $www= $wrong['count'];
            $this->token_data['success'] = "密码错误";
            $this->token_data['login'] = "0";
        }
    }



}